package com.pactera.madp.cp.common.util;

import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONUtil;
import com.pactera.madp.cp.common.constant.WxGZHApiConst;
import com.pactera.madp.cp.service.WechatComponentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

@Slf4j
public class GzhApiUtils {

    @Autowired
    private static WechatComponentService wechatComponentService;

    /**
     * gzh 设置所属行业
     * @param obj
     * @return
     */
    public static String setIndustry(Object obj) {
        String token = wechatComponentService.getComponentAccessToken();
        String msgUrl = WxGZHApiConst.API_POST_SET_INDUSTRY + "?access_token=" + token;
        String respJson = HttpUtil.post(msgUrl, JSONUtil.parseObj(obj));
        return respJson;
    }

    /**
     * 发送公众号模板消息
     * @param obj
     * @return
     */
    public static String sendGZHMsg(Object obj) {
        String token = wechatComponentService.getComponentAccessToken();
        String msgUrl = WxGZHApiConst.API_POST_GZH_MESSAGE + "?access_token=" + token;
        String reqBody = JSONUtil.toJsonStr(obj);
        String respJson = HttpUtil.post(msgUrl, reqBody);
        return respJson;
    }

    /**
     * gzh 获得模板id
     * @param obj
     * @return
     */
    public static String getTemplate(Object obj) {
        String token = wechatComponentService.getComponentAccessToken();
        String msgUrl = WxGZHApiConst.API_POST_GZH_TEMPLATE + "?access_token=" + token;
        String reqBody = JSONUtil.toJsonStr(obj);
        String respJson = HttpUtil.post(msgUrl, reqBody);
        return respJson;
    }

    /**
     * 获取所有的订阅用户
     * @param next_openid
     * @return
     */
    public static String getAllUser(String next_openid) {
        String token = wechatComponentService.getComponentAccessToken();
        StringBuffer msgUrl = new StringBuffer();
        msgUrl.append(WxGZHApiConst.API_GET_GZH_GETALLUSER + "?access_token=" + token);
        if (next_openid != null) msgUrl.append("&next_openid=" + next_openid);
        String respJson = HttpUtil.get(msgUrl.toString());
        return respJson;
    }

    public static String getUserByOpenId(String openId) {
        String token = wechatComponentService.getComponentAccessToken();
        String url = WxGZHApiConst.API_GET_GZH_USER_INFO;
        url.replace("ACCESS_TOKEN", token);
        url.replace("OPENID", openId);
        String respJson = HttpUtil.get(url);
        return respJson;
    }
}
